package cn.volunteer.points.system.service.impl;

import cn.volunteer.points.system.handle.ServiceErrorCode;
import cn.volunteer.points.system.handle.ServiceException;
import cn.volunteer.points.system.mapper.ActivityMapper;
import cn.volunteer.points.system.mapper.UserActivityMapper;
import cn.volunteer.points.system.pojo.entity.Activity;
import cn.volunteer.points.system.pojo.entity.UserActivity;
import cn.volunteer.points.system.pojo.qo.UserActivityQO;
import cn.volunteer.points.system.pojo.vo.ActivityVO;
import cn.volunteer.points.system.service.ActivityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;

/**
 * @description: 请输入描述信息
 * @author:
 * @create: 创建时间：2021-04-24
 **/
@Slf4j
@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper ;

    @Autowired
    private UserActivityMapper userActivityMapper ;

    @Override
    public String add(Activity activity) {
        ActivityVO byName = activityMapper.findByName(activity.getActivityName());
        if (!ObjectUtils.isEmpty(byName)) {
            throw new ServiceException(ServiceErrorCode.NAME_EXEIT);
        }
        activityMapper.add(activity);
        return "活动创建成功！！！";
    }

    @Override
    public String update(Activity activity) {
//        Activity byName = activityMapper.findByName(activity.getActivityName());
//        if (!ObjectUtils.isEmpty(byName)) {
//            throw new ServiceException(ServiceErrorCode.NAME_EXEIT);
//        }
        activityMapper.update(activity);
        return "活动修改成功！！！";
    }

    @Override
    public void del(Integer activityId) {
        activityMapper.del(activityId);
    }

    @Override
    public ActivityVO findById(Integer activityId) {
        return activityMapper.findById(activityId);
    }

    @Override
    public ActivityVO findByName(String activityName) {
        return activityMapper.findByName(activityName);
    }

    @Override
    public List<ActivityVO> activityList(Integer status) {
        return activityMapper.list(status);
    }

    @Override
    public String userFinishActivity(UserActivity userActivity) {

        UserActivity activity = userActivityMapper.getUserActivity(userActivity);
        if (activity == null) {
            userActivityMapper.add(userActivity);
            return "参加活动成功！！！";
        }
        userActivityMapper.update(userActivity);
        return "完成活动成功！！！";

    }

    @Override
    public List<Map> userActivityInfo(Integer userId) {
        return userActivityMapper.list(userId);
    }

    @Override
    public String userCancelActivtiy(UserActivityQO userActivityQO) {

        List<Integer> activityIds = userActivityQO.getActivityIds();

        activityIds.stream().forEach(a->{
            userActivityMapper.del(userActivityQO.getUserId(),a);
        });

        return "已取消参加活动";
    }

    @Override
    public Integer getUserActivityIntegral(Integer userId) {
        return userActivityMapper.getUserActivityIntegral(userId);
    }
}
